﻿using BLL;
using Model;
using System;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditEntrust : FormPage
    {
        private Qs_EntrustBLL entrustBLL = new Qs_EntrustBLL();
        private Sys_UserBLL userBLL = new Sys_UserBLL();

        protected override void SaveClick(object sender, EventArgs e)
        {
            decimal principal;
            if (string.IsNullOrEmpty(HIDE_FORM_PRINCIPAL.Value))
            {
                MessageBox.Show(this, "请选择委托人");
                return;
            }
            else
            {
                principal = Convert.ToDecimal(HIDE_FORM_PRINCIPAL.Value);
            }
            decimal userid;
            if (string.IsNullOrEmpty(HIDE_FORM_USER_ID.Value))
            {
                MessageBox.Show(this, "请选择被委托人");
                return;
            }
            else
            {
                userid = Convert.ToDecimal(HIDE_FORM_USER_ID.Value);
            }
            if (FORM_COMPANY.Value == -1)
            {
                MessageBox.Show(this, "请选择公司");
                return;
            }
            if (FORM_BEGIN_DATE.Value == "")
            {
                MessageBox.Show(this, "请选择开始时间");
                return;
            }
            if (FORM_OVER_DATE.Value == "")
            {
                MessageBox.Show(this, "请选择结束时间");
                return;
            }
            if (FORM_IFEDIT)
            {
                #region 修改

                var entity = entrustBLL.Find(FORM_KEY);
                entity.N_Principal = principal;
                entity.N_User_Id = userid;
                entity.D_Ntrust_start_date = Convert.ToDateTime(FORM_BEGIN_DATE.Value);
                entity.D_ntrust_enddata = Convert.ToDateTime(FORM_OVER_DATE.Value);
                entity.C_Entrust_state = FORM_STATE.SelectedValue;
                entity.N_Entrust_remarks = FORM_REMARK.Value;
                entity.Company_Id = FORM_COMPANY.Value;
                entity.D_ntrust_operating_time = DateTime.Now;
                if (entrustBLL.Update(entity))
                {
                    MessageBox.Show(this, "修改成功");
                }
                else
                {
                    MessageBox.Show(this, "修改异常");
                }

                #endregion 修改
            }
            else
            {
                #region 新增

                var entity = new Qs_Entrust();
                entity.N_Principal = principal;
                entity.N_User_Id = userid;
                entity.D_Ntrust_start_date = Convert.ToDateTime(FORM_BEGIN_DATE.Value);
                entity.D_ntrust_enddata = Convert.ToDateTime(FORM_OVER_DATE.Value);
                entity.C_Entrust_state = FORM_STATE.SelectedValue;
                entity.N_Entrust_remarks = FORM_REMARK.Value;
                entity.Company_Id = FORM_COMPANY.Value;
                entity.D_ntrust_operating_time = DateTime.Now;
                if (entrustBLL.Insert(entity))
                {
                    MessageBox.Show(this, "新增成功");
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Modal/EditEntrust.aspx?IF_EDIT=TRUE&ID=" + entity.N_Entrust_Id);
                }
                else
                {
                    MessageBox.Show(this, "新增异常");
                }

                #endregion 新增
            }
        }

        protected override void BindPage()
        {
            if (FORM_IFEDIT)
            {
                var entity = entrustBLL.Find(FORM_KEY);
                var principalUser = userBLL.Find(entity.N_Principal);
                if (principalUser != null)
                {
                    FORM_PRINCIPAL.Value = principalUser.USER_NAME;

                    HIDE_FORM_PRINCIPAL.Value = principalUser.USER_ID.ToString();
                }
                var user = userBLL.Find(entity.N_User_Id);
                if (user != null)
                {
                    FORM_USER_ID.Value = user.USER_NAME;
                    HIDE_FORM_USER_ID.Value = user.USER_ID.ToString();
                }
                FORM_BEGIN_DATE.Value = entity.D_Ntrust_start_date.ToString("yyyy-MM-dd HH:mm:ss");
                FORM_OVER_DATE.Value = entity.D_ntrust_enddata.ToString("yyyy-MM-dd HH:mm:ss");
                FORM_STATE.SelectedValue = entity.C_Entrust_state;
                FORM_REMARK.Value = entity.N_Entrust_remarks;
                D_NTRUST_OPERATING_TIME.InnerText = entity.D_ntrust_operating_time.ToString("yyyy-MM-dd HH:mm:ss");
                FORM_COMPANY.Value = entity.Company_Id;
            }
            else
            {
                Div.Visible = false;
            }
        }

        //protected override void UpdateFormSize()
        //{
        //    MessageBox.ResponseScript(this, "FORM_SIZE(800,500);");
        //}
        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"编辑委托设置\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加委托设置\");");
            }
        }
    }
}